package com.wipz.eoa.iphone.costmanagent;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.opensymphony.xwork2.ActionSupport;
import com.toft.utils.UUIDHexGenerator;
import com.toft.utils.json.JSONObject;
import com.toft.widgets.workflow.utils.DateConvertUtil;
import com.wipz.eoa.iphone.BaseAction;
import com.wipz.eoa.iphone.costmanagement.ICostListService;
import com.wipz.eoa.iphone.costmanagent.proivderData.DadilyCostData;
import com.wipz.eoa.iphone.costmanagent.proivderData.LodgingDetailData;
import com.wipz.eoa.iphone.costmanagent.proivderData.OtherDetailData;
import com.wipz.eoa.iphone.costmanagent.proivderData.PartyDetailData;
import com.wipz.eoa.iphone.costmanagent.proivderData.PurchaseDetailData;
import com.wipz.eoa.iphone.costmanagent.proivderData.TrifficDetailData;
import com.wipz.eoa.iphone.util.Constants;

public class DeailyListOfCostAction extends BaseAction{
	/*return TreeJson   */
	private Map deailyListOfCost;
	
	/* return costlist*/
	private Map getDailyList;


	public Map getGetDailyList() {
		return getDailyList;
	}

	public void setGetDailyList(Map getDailyList) {
		this.getDailyList = getDailyList;
	}

	private static final long serialVersionUID = 1L;
	/* service */
	private ICostListService costListService;
	
	public void setCostListService(ICostListService costListService) {
		this.costListService = costListService;
	}
	
	public String deailyListOfCost(){
		String node=ServletActionContext.getRequest().getParameter("node").trim();
		String param=ServletActionContext.getRequest().getParameter("param").trim();
		if(param.split(",").length>2){
			node=param.split(",")[2];
		}
		try{
			deailyListOfCost=new HashMap();
			if("root".equals(node)){
				deailyListOfCost.put("deailyListOfCost", CostdetailMenu.getMenuBytype(node, param));
			}else{
				deailyListOfCost.put("deailyListOfCost", costListService.queryTreeStoreList(node,param));
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public Map getDeailyListOfCost() {
		return deailyListOfCost;
	}

	public void setDeailyListOfCost(Map deailyListOfCost) {
		this.deailyListOfCost = deailyListOfCost;
	}

	public Map insert;
	public String saveDeailyCost(){
		try{
			insert=new HashMap();
			String type=this.getRequest().getParameter("Type");
			String COMMITERCODE=this.getRequest().getParameter("COMMITERCODE");
			Map objmap=(new DadilyCostData()).getDadilyCostData(this.getRequest());
			objmap.put("COMMITERCODE", COMMITERCODE);
			if("002".equals(type)){
				costListService.insert("travelReimbursement.insert",objmap);
			}else if("001".equals(type)){
				costListService.insert("regularexpenses.insert",objmap);
			}else if("003".equals(type)){
				costListService.insert("purchase_reimbursement.insert",objmap);
			}
			insert.put("FORM", objmap);
			insert.put("success",true);
			insert.put("msg", "保存成功");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public Map dailyCostUpdate;
	public String dailyCostUpdate(){
		try{
			dailyCostUpdate=new HashMap();
			String[] reqparam=this.getRequest().getParameter("param").split(",");
			Map objmap=(new DadilyCostData()).getDadilyCostData(this.getRequest());	
			objmap.remove("BILLCODE");objmap.put("BILLCODE",reqparam[1]);
			objmap.put("TABLE", CostDetailProperties.returnTableNameBytype(Integer.parseInt(reqparam[0])));
			costListService.update("CostDetail.update", objmap);
			dailyCostUpdate.put("success", true);
			dailyCostUpdate.put("msg","修改成功！");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String getDailyList(){
		Map param=new HashMap();
		getDailyList=new HashMap();
		getDailyList.put("getDailyList", costListService.getDailyList(param));
		return SUCCESS;
	}
	
	public Map getCostDetailByBillcode;
	public Map getGetCostDetailByBillcode() {
		return getCostDetailByBillcode;
	}

	public void setGetCostDetailByBillcode(Map getCostDetailByBillcode) {
		this.getCostDetailByBillcode = getCostDetailByBillcode;
	}

	public String getCostDetailByBillcode(){		
		String BILLCODE=this.getRequest().getParameter("BILLCODE");
		String type=this.getRequest().getParameter("TYPE");
		String Billcode=null;
		if(!"undefined".equals(BILLCODE)){
		    Billcode=BILLCODE;		
		}
		Map param=new HashMap();
		param.put("BILLCODE", Billcode);
		getCostDetailByBillcode=new HashMap();
		if(Billcode!=null && Billcode.length()>0){
			if("001".equals(type)){
				getCostDetailByBillcode=DadilyCostData.CovertSqlDateToUtilDate((HashMap)costListService.queryOne("travelReimbursementDetail.query", param));
			}else if("002".equals(type)){
				getCostDetailByBillcode=DadilyCostData.CovertSqlDateToUtilDate((HashMap)costListService.queryOne("regularexpensesDetail.query", param));
			}else if("003".equals(type)){
				getCostDetailByBillcode=DadilyCostData.CovertSqlDateToUtilDate((HashMap)costListService.queryOne("purchase_reimbursementDetail.query", param));
			}
		}else{
			
			String COMMITERCODE=this.getRequest().getParameter("COMMITERCODE");
			String loginname = (String) costListService.queryOne("user.queryName", COMMITERCODE);
			getCostDetailByBillcode.put("COMMITER", loginname);
			getCostDetailByBillcode.put("COMMITERCODE", COMMITERCODE);

			String userid = (String) costListService.queryOne("user.queryId", COMMITERCODE);
			Map department =  (Map) costListService.queryOne("agency.queryAgencyByUserID", userid);
			if(null != department) {
				String deparmentname = (String) department.get("NAME");
				getCostDetailByBillcode.put("DEPARTMENT", deparmentname);
			}
		}
		return SUCCESS;
	}
	
	public String deleteByBillcode(){
		String[] reqparam=this.getRequest().getParameter("param").split(",");
		Map param=new HashMap();
		param.put("BILLCODE", reqparam[1]);
		if("002".equals(reqparam[0])){
			costListService.delete("travelReimbursement.delete", param);
		}else if("001".equals(reqparam[0])){
			costListService.delete("regularexpenses.delete", param);
		}else if("003".equals(reqparam[0])){
			costListService.delete("purchase_reimbursement.delete", param);
		}		
		return SUCCESS;
	}

	public Map getInsert() {
		return insert;
	}

	public void setInsert(Map insert) {
		this.insert = insert;
	}

	public Map getDailyCostUpdate() {
		return dailyCostUpdate;
	}

	public void setDailyCostUpdate(Map dailyCostUpdate) {
		this.dailyCostUpdate = dailyCostUpdate;
	}
	
	public Map saveDetailyCost;
	public Map getSaveDetailyCost() {
		return saveDetailyCost;
	}

	public void setSaveDetailyCost(Map saveDetailyCost) {
		this.saveDetailyCost = saveDetailyCost;
	}

	public String saveDetailyCost(){
		try{
			Map pm=new HashMap();
			saveDetailyCost=new HashMap();
			String param[]=this.getRequest().getParameter("reqparam").split(",");
			if(param.length>3){
				pm.put("ID", param[3]);
				pm.put("TABLE", TracelProperties.returnDeatilTableNameByType(Integer.parseInt(param[2])));
				saveDetailyCost.put("saveDetailyCost", costListService.queryOne("detaily.query", pm));
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public Map detailInsert;
	public Map getDetailInsert() {
		return detailInsert;
	}

	public void setDetailInsert(Map detailInsert) {
		this.detailInsert = detailInsert;
	}

	public String detailInsert(){
		String param[]=this.getRequest().getParameter("reqparam").split(",");
		try{
			detailInsert=new HashMap();
			Map objmap= new HashMap();
			switch(Integer.parseInt(param[2])){
			case 001:
				objmap=(new TrifficDetailData()).getTrifficDetailData(this.getRequest(),param);
				costListService.insert("detailyTriffic.insert",objmap);
				break;
			case 002:
				objmap=(new PartyDetailData()).getTrifficDetailData(this.getRequest(),param);
				costListService.insert("detailyParty.insert",objmap);
				break;
			case 003:
				objmap=(new OtherDetailData()).getOtherDetailData(this.getRequest(),param);
				costListService.insert("detailyother.insert",objmap);
				break;
			case 004:
				objmap=(new LodgingDetailData()).getLodgingDetailData(this.getRequest(),param);
				costListService.insert("detailyLodging.insert",objmap);
				break;
			case 005:
				objmap=(new PurchaseDetailData()).getPurchaseDetailData(this.getRequest(),param);
				costListService.insert("detailyPurchase.insert",objmap);
				break;
			default:
				break;				
			}
			
			objmap.put("ID", param[1]);
			objmap.put("TABLE", TracelProperties.returnTableNameBytype(Integer.parseInt(param[0])));
			costListService.update("costCountAmount.update", objmap);
			detailInsert.put("success", true);
			detailInsert.put("msg", "保存成功");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public Map detailUpdate;
	public String detailUpdate(){
		String param[]=this.getRequest().getParameter("reqparam").split(",");
		try{
			detailUpdate=new HashMap();
			Map objmap=new HashMap();
			switch (Integer.parseInt(param[2])) {
			case 001:
				objmap=(new TrifficDetailData()).getTrifficDetailData(this.getRequest(),param);
				objmap.put("ID", param[3]);
				costListService.update("detailyTriffic.update",objmap);
				break;
			case 002:
				objmap=(new PartyDetailData()).getTrifficDetailData(this.getRequest(),param);
				objmap.put("ID", param[3]);
				costListService.update("detailyParty.update",objmap);
				break;
			case 003:
				objmap=(new OtherDetailData()).getOtherDetailData(this.getRequest(),param);
				objmap.put("ID", param[3]);
				costListService.update("detailyother.update",objmap);
				break;
			case 004:
				objmap=(new LodgingDetailData()).getLodgingDetailData(this.getRequest(),param);
				objmap.put("ID", param[3]);
				costListService.update("detailyLodging.update",objmap);
				break;
			case 005:
				objmap=(new PurchaseDetailData()).getPurchaseDetailData(this.getRequest(),param);
				objmap.put("ID", param[3]);
				costListService.update("detailyPurchase.update",objmap);
				break;
			default:
				break;				
			}
			
			objmap.put("ID", param[1]);
			objmap.put("TABLE",TracelProperties.returnTableNameBytype(Integer.parseInt(param[0])));
			costListService.update("costCountAmount.update", objmap);
			detailUpdate.put("success", true);
			detailUpdate.put("msg", "修改成功");
			
		}catch(Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public Map deleteDetail;
	public String deleteDetail(){
		try{
			Map pm=new HashMap();
			Map updatemap;
			deleteDetail=new HashMap();
			String param[]=this.getRequest().getParameter("reqparam").split(",");
			if(param.length>3){
				pm.put("ID", param[3]);
				pm.put("TABLE", TracelProperties.returnDeatilTableNameByType(Integer.parseInt(param[2])));
				deleteDetail.put("deleteDetail", costListService.delete("deletedetaily.query", pm));
				
				updatemap=new HashMap();
				updatemap.put("ID",param[1]);
				updatemap.put("TABLE",TracelProperties.returnTableNameBytype(Integer.parseInt(param[0])));
				costListService.update("detailyPurchase.update",updatemap);
				deleteDetail.put("success", true);
			}
			
		}catch (Exception e){
			e.printStackTrace();
		}
		return SUCCESS;
	}
	public Map getDetailUpdate() {
		return detailUpdate;
	}

	public void setDetailUpdate(Map detailUpdate) {
		this.detailUpdate = detailUpdate;
	}
	
	public Map getRequestUser;
	public String getRequestUser(){
		getRequestUser=new HashMap();
		Map map=new HashMap();
		map.put("COMMITERCODE", this.getRequest().getParameter("COMMITERCODE"));
		getRequestUser.put("requestuserlist", costListService.queryList("user.queryrequestAll", map));
		return SUCCESS;
	}
}